<?php
/**
 * @特惠赚、首页控制器API
 * Author: 小谢
 * Date: 2019-5-13
 */
namespace app\api\controller;
use app\api\model\Goods;
use app\api\logic\PageLogic;

class Index extends Base
{

    /**
     * @breif 首页统一接口返回
     */
    public function unifyIndexApi() {
        $Goods = new Goods();
        //首页展示分类
        $cateList = $Goods->getCategoryList(1,['id','name','image'],'sort_order ASC');
        //轮播图片
        $fields = ['ad_id','ad_name','ad_link','ad_code'];
        $adList   = M('ad')->where(['enabled'=>1])->field($fields)->order('orderby ASC')->select();
        //友情链接
        $linksList= M('links')->where(['is_show'=>1])->order('sort_order ASC')->select();

        //文章新闻头条
        $articleList= M('article')->where(['is_open'=>1])->field('article_id,title')->order('sort ASC')->limit(5)->select();

        $list = [
            'cateList'=>$cateList,
            'adList'=>$adList,
            'linksList'=>$linksList,
            'articleList'=>$articleList
        ];
        $this->ajaxReturn($this->callBack(1,0,'成功',$list),'JSON');
    }

    /**
     * 推荐商品列表
     */
    public function recommendList() {
        $condition['is_recommend'] = 1;
         //分页参数
        $page = I('get.page') ? intval(I('get.page')) : 1 ;
        $pageSize = 20;
        $count = M('goods')->where($condition)->count();
        $PageLogic = new PageLogic($page,$pageSize,$count);
        $pageInfo = $PageLogic->pageHandle();//分页操作
        $fields = ['goods_id','title','pic_url','price'];
        $goodsList = M('goods')->where($condition)->field($fields)->order('add_time DESC')->limit($pageInfo['pageStart'],$pageInfo['pageSize'])->select();
        if($goodsList) {
            $this->ajaxReturn($this->callBack(1,0,'成功',['page'=>$page,'pageSize'=>$pageSize,'totalPage'=>$pageInfo['totalPage'],'list'=>$goodsList]),'JSON');
        }
        $this->ajaxReturn($this->callBack(0,0,'暂无推荐商品',self::emptyObject()),'JSON');
    }
}
